<template>
  <el-dialog
    title="分配角色"
    :visible.sync="dialogVisible"
    @close="btnCancel"
  >
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="btnOK">确 定</el-button>
      <el-button @click="btnCancel">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getRoleList } from '@/api/setting'
import { getUserDetails } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 所有的角色列表
      roleIds: [] // 当前员工的角色
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async  getRoleList() {
      const res = await getRoleList()
      //   console.log(res)
      this.list = res.rows
    },
    // 获取当前 员工 拥有的角色
    async  getUserDetails(id) {
      const res = await getUserDetails(id)
      //   console.log(res.roleIds)
      this.roleIds = res.roleIds
    },
    // 点击取消
    btnCancel() {
      this.roleIds = [] // 清空原来的数组
      this.$emit('update:dialogVisible', false)
    },
    // 点击确定
    async  btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$emit('update:dialogVisible', false)
      this.$message.success('分配成功')
    }
  }
}
</script>
<style>

</style>
